home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Sessions / STL / Slides / STL6.cp < prev    next >
Text File  |  1998-06-15  |  1KB  |  55 lines

  1. // STL6.cp
  2. #include <iostream>
  3. #include <deque>
  4. using namespace std;
  5. /*
  6.  
  7.     The exception hierarchy:
  8.  
  9.         exception            root of all exceptions
  10.         |    |
  11.         |    bad_alloc        memory allocation exception
  12.         |
  13.         logic_error            precondition violation exception root
  14.         |    |    |
  15.         |    |    out_of_range    attempt to reference a container
  16.         |    |                    using an illegal index
  17.         |    |
  18.         |    length_error    attempt to create a string with an
  19.         |                    illegal length
  20.         |
  21.         invalid_argument    attempt ot create a container with an
  22.                             illegal size such as -1
  23. */
  24.  
  25. int main()
  26. {
  27.     const    int    limit = 10;
  28.     vector<int> v;
  29.     for (int j = 0; j < limit; ++j)
  30.     {
  31.         v.push_back(j);
  32.     }
  33.     try
  34.     {
  35.         int i = v[limit];
  36.         cout << i << " no exception for i = v[limit];" << endl;
  37.         i = v.at(limit);
  38.         cout << i << " no exception i = v.at(limit);" << endl;
  39.         i = v.at(limit + 1);
  40.         cout << i << " no exception i = v.at(limit + 1);" << endl;
  41.     }
  42.     catch (out_of_range& error)
  43.     {
  44.         cout << "An out of range execption was caught" << endl;
  45.     }
  46. }
  47. // CW Pro 1 output
  48. // 1868783980 no exception for i = v[limit];
  49. // 1868783980 no exception i = v.at(limit);
  50. // An out of range execption was caught
  51.  
  52. // CW Pro 3 output
  53. // -8 no exception for i = v[limit];
  54. // An out of range execption was caught
  55.